package com.wptk.filemanager.ui.base;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.gyf.immersionbar.ImmersionBar;
import com.wptk.filemanager.R;
import com.wptk.filemanager.ui.file.DeleteDialogCallBackListener;
import com.wptk.filemanager.ui.file.RenameDialogCallBackListener;
import com.wptk.filemanager.utils.DensityUtil;
import com.wptk.filemanager.utils.Utils;

import java.io.File;
import java.util.List;

/**
 * @description: Activity 基类
 * @date 2022/5/24 17:00
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected final String TAG = getClass().getSimpleName();
    protected Toolbar toolbar;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        setContentView(getLayoutId());
        toolbar = findViewById(R.id.toolbar);
        if (enableImmersion()) {
            ImmersionBar.with(this)
                    //.statusBarColor(android.R.color.transparent)
                    .statusBarView(R.id.status_bar_dimen)
                    .statusBarDarkFont(true)
                    .init();
        }

        initView();
        initEvent();

        if(toolbar != null){
            setSupportActionBar(toolbar);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    /**
     * 设置布局id
     * @return 布局ID
     */
    protected abstract int getLayoutId();

    /**
     * 寻找控件
     */
    protected abstract void initView();

    /**
     * 控件事件
     */
    protected abstract void initEvent();

    protected void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    protected void migrateTo(Class clazz) {
        startActivity(new Intent(this, clazz));
    }

    protected boolean enableImmersion() {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    /**
     * 通知系统 文件已更新
     * @param oldFilePath 文件路径
     * @param newFilePath 新文件路径
     */
    public void notifyFileNameChanged(String oldFilePath,String newFilePath){
        final Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        final Uri uri = Uri.fromFile(new File(newFilePath));
        intent.setData(uri);
        sendBroadcast(intent);
        MediaScannerConnection.scanFile(this,new String[]{oldFilePath,newFilePath},null,null);
    }

    public void notifyFileIsDeleted(List<String> filePaths){
        if (filePaths.size() <= 0) return;
        MediaScannerConnection.scanFile(this, filePaths.toArray(new String[filePaths.size()]),null,null);
    }
}
